package com.tnl.app.dao;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.tnl.app.entity.OrderDesc;
import com.tnl.base.BaseDAO;

@Repository
public class OrderDescDAO {
	private static final String SQL_GET_ID = "select * from tb_orderdesc  where id = ?";
	private static final String SQL_GET_NAME = "select * from tb_orderdesc  where name = ?";
	private static final String SQL_GET_NAME_ID = "SELECT * FROM tb_orderdesc  WHERE name = ? AND id <>?";
	
	private static final String SQL_GET_ALL = "select * from tb_orderdesc";
	private static final String SQL_GET_ALL_WITH_LIMIT = "select * from tb_orderdesc where 1=1 ";
	private static final String SQL_GET_ALL_COUNT = "SELECT COUNT(*) FROM tb_orderdesc where 1=1 ";
	
	private static final String SQL_SAVE = "INSERT INTO tb_orderdesc(name, spec,tag,remark) VALUES(?, ?,?,?)";
	private static final String SQL_UPDATE = "UPDATE tb_orderdesc SET name=?, spec=?,tag=?,remark=? WHERE id=?";
	private static final String SQL_DELETE ="DELETE FROM tb_orderdesc WHERE id=?";

	public Long getCountOfAll(String name, Integer postype) {
		String sql = SQL_GET_ALL_COUNT;
		if (StringUtils.hasText(name)) {
			sql += " and name like '" + name + "'";
		}
		if (postype != null) {
			sql += " and spec =" + postype + " ";
		}
		return (Long) BaseDAO.queryOneRowOneCol(sql);
	}

	public List<OrderDesc> getAllWithLimit(String name, Integer postype, 
			long start, long length) {
		String sql = SQL_GET_ALL_WITH_LIMIT;
		if (StringUtils.hasText(name)) {
			sql += " and name like '" + name + "'";
		}
		if (postype != null) {
			sql += " and spec =" + postype + " ";
		}
		
		sql += " LIMIT ?, ?";
		return BaseDAO.queryForList(OrderDesc.class, sql, start, length);
	}

	public List<OrderDesc> getALL() {
		return BaseDAO.queryForList(OrderDesc.class, SQL_GET_ALL);
	}

	public OrderDesc getById(long Id) {
		return BaseDAO.queryForObject(OrderDesc.class, SQL_GET_ID, Id);
	}


	public OrderDesc getByName(String name) {
		return BaseDAO.queryForObject(OrderDesc.class, SQL_GET_NAME, name);
	}

	public OrderDesc getByNameAndNotId(String name, long id) {
		return BaseDAO.queryForObject(OrderDesc.class, SQL_GET_NAME_ID, name, id);
	}

	public int delete(long ordescid) {
		if (ordescid <=0) {
			return -1;
		}
		return BaseDAO.execute(SQL_DELETE, ordescid);
	}
	
	public int update(OrderDesc orderdesc) {
		return BaseDAO.execute(SQL_UPDATE, orderdesc.getName(), orderdesc.getSpec(),orderdesc.getTag(),orderdesc.getRemark(),orderdesc.getId());
	}
	
	public long save(OrderDesc orderdesc) {
		if (orderdesc == null) {
			return -1;
		}

		Long result = BaseDAO.save(SQL_SAVE, orderdesc.getName(), orderdesc.getSpec(),orderdesc.getTag(),orderdesc.getRemark());
		if (result != null) {
			return result.longValue();
		}
		return -1;
	}
	
	private static final String SQL_GET_TAG = "select b.id,b.name,b.spec,b.unit,b.tag,b.remark from tb_hunter_desc a left join tb_orderdesc b ON a.orderdescId=b.id where a.isdelete=0 and a.hunterId=? and b.tag=? order by b.id asc";
	public List<OrderDesc> getByTag(Integer hunterid,String tag) {
		return BaseDAO.queryForList(OrderDesc.class, SQL_GET_TAG, hunterid,tag);
	}

}
